home *** CD-ROM | disk | FTP | other *** search
/ Pesquisa Dirigida / Pesquisa Dirigida.iso / JOGOS / parking.swf / scripts / frame_1 / DoAction.as
Text File  |  2005-01-18  |  16KB  |  583 lines

  1. function Game(timeline, gametype)
  2. {
  3.    this.gametype = gametype;
  4.    this.timeline = timeline;
  5.    this.scores = new Array();
  6.    this.finalscore = new Number();
  7. }
  8. function Car()
  9. {
  10.    this.rotation = this._rotation;
  11.    this.carx = this._x;
  12.    this.cary = this._y;
  13.    this.delta = 16900;
  14. }
  15. function createGame(gametype)
  16. {
  17.    if(_global.game == undefined)
  18.    {
  19.       _global.game = new Game(this,gametype);
  20.    }
  21.    else
  22.    {
  23.       _global.game.gametype = gametype;
  24.    }
  25. }
  26. function playButtonClick()
  27. {
  28.    this._parent._parent.fadeIntroSound();
  29.    _global.player_gender = this._name;
  30.    this._parent._parent.createGame(1);
  31.    this._parent._parent.gotoAndStop("game1");
  32. }
  33. function startPractice()
  34. {
  35.    this.createGame(0);
  36.    this.gotoAndStop("game0");
  37. }
  38. function fadeIntroSound()
  39. {
  40.    this.onEnterFrame = function()
  41.    {
  42.       this.audio_mc.intro_sound.setVolume(this.audio_mc.intro_sound.getVolume() - 5);
  43.       if(this.audio_mc.intro_sound.getVolume() <= 0)
  44.       {
  45.          this.audio_mc.intro_sound.stop();
  46.          delete this.audio_mc.intro_sound;
  47.          delete this.onEnterFrame;
  48.       }
  49.    };
  50. }
  51. stop();
  52. Game.prototype = new Object();
  53. Game.prototype.startGame = function()
  54. {
  55.    var xpos = this.timeline.carplacer._x;
  56.    var ypos = this.timeline.carplacer._y;
  57.    var depth = this.timeline.carplacer.getDepth();
  58.    var rotation = this.timeline.carplacer._rotation;
  59.    switch(this.gametype)
  60.    {
  61.       case 0:
  62.          this.timeline.attachMovie("blue_hatchback","car_mc",depth,{_x:xpos,_y:ypos,_rotation:rotation});
  63.          this.timeline.car_mc.init(0.6,6,0.4,2,0,20);
  64.          break;
  65.       case 1:
  66.          this.timeline.attachMovie("blue_hatchback","car_mc",depth,{_x:xpos,_y:ypos,_rotation:rotation});
  67.          this.timeline.car_mc.init(0.6,6,0.4,2,3,20);
  68.          this.timeline.attachMovie("countdownclock","timer_mc",1,{_x:435,_y:5.5});
  69.          this.timeline.timer_mc.setChangeHandler("gameOver",this);
  70.          this.timeline.timer_mc.setData(0,60,false);
  71.          break;
  72.       case 2:
  73.          this.timeline.attachMovie("black_saloon","car_mc",depth,{_x:xpos,_y:ypos,_rotation:rotation});
  74.          this.timeline.car_mc.init(0.7,7,0.34,10,2,20);
  75.          this.timeline.timer_mc.reset(false);
  76.          break;
  77.       case 3:
  78.          this.timeline.attachMovie("grey_suv","car_mc",depth,{_x:xpos,_y:ypos,_rotation:rotation});
  79.          this.timeline.car_mc.init(0.5,5,0.2,7,0,20);
  80.          this.timeline.timer_mc.reset(false);
  81.    }
  82. };
  83. Game.prototype.nextGame = function()
  84. {
  85.    this.gametype = this.gametype + 1;
  86.    this.timeline.gotoAndStop("game" + this.gametype);
  87. };
  88. Game.prototype.gameOver = function()
  89. {
  90.    this.timeline.car_mc.stopEngine();
  91.    this.calculateScore(0,0,0,true);
  92. };
  93. Game.prototype.retryPractice = function()
  94. {
  95.    this.timeline.car_mc.init(0.6,6,0.4,2,0,16);
  96. };
  97. Game.prototype.calculateScore = function(accuracy, hitcar, hitkerb, blnOutOfTime)
  98. {
  99.    if(this.gametype > 0)
  100.    {
  101.       if(this.scores[this.gametype] == undefined)
  102.       {
  103.          this.scores[this.gametype] = new Object();
  104.       }
  105.       if(!blnOutOfTime)
  106.       {
  107.          this.scores[this.gametype].accuracy = accuracy;
  108.          this.scores[this.gametype].hitcar = hitcar;
  109.          this.scores[this.gametype].hitkerb = hitkerb;
  110.          this.scores[this.gametype].timetaken = 60 - this.timeline.timer_mc.getTimeRemaining();
  111.          if(accuracy == 0)
  112.          {
  113.             this.scores[this.gametype].total = 0;
  114.          }
  115.          else
  116.          {
  117.             this.scores[this.gametype].total = accuracy - hitcar * 20 - hitkerb * 5 + this.timeline.timer_mc.getTimeRemaining();
  118.          }
  119.          if(this.scores[this.gametype].total < 0)
  120.          {
  121.             this.scores[this.gametype].total = 0;
  122.          }
  123.          this.displayScore(false);
  124.       }
  125.       else
  126.       {
  127.          this.scores[this.gametype].accuracy = 0;
  128.          this.scores[this.gametype].hitcar = 0;
  129.          this.scores[this.gametype].hitkerb = 0;
  130.          this.scores[this.gametype].timetaken = 0;
  131.          this.scores[this.gametype].total = 0;
  132.          if(this.scores[this.gametype].total < 0)
  133.          {
  134.             this.scores[this.gametype].total = 0;
  135.          }
  136.          this.displayScore(true);
  137.       }
  138.    }
  139.    else
  140.    {
  141.       this.timeline.attachMovie("retry practice","retry_mc",15,{_x:352,_y:12});
  142.    }
  143. };
  144. Game.prototype.displayScore = function(blnOutOfTime)
  145. {
  146.    if(blnOutOfTime)
  147.    {
  148.       this.timeline.attachMovie("scorepanel","scorepanel_mc",5,{_x:122,_y:10,outoftime:true});
  149.    }
  150.    else
  151.    {
  152.       this.timeline.attachMovie("scorepanel","scorepanel_mc",5,{_x:122,_y:10,outoftime:false});
  153.    }
  154. };
  155. Game.prototype.getFinalScore = function()
  156. {
  157.    var i = 1;
  158.    while(i < this.scores.length)
  159.    {
  160.       this.finalscore += this.scores[i].total;
  161.       i++;
  162.    }
  163.    this.timeline.timer_mc.removeMovieClip();
  164.    this.timeline.gotoAndStop("submitscore");
  165. };
  166. Game.prototype.reset = function()
  167. {
  168.    this.gametype = undefined;
  169.    this.finalscore = 0;
  170.    var i = 0;
  171.    while(i < 4)
  172.    {
  173.       this.scores[i].accuracy = 0;
  174.       this.scores[i].hitcar = 0;
  175.       this.scores[i].hitkerb = 0;
  176.       this.scores[i].timetaken = 0;
  177.       this.scores[i].total = 0;
  178.       i++;
  179.    }
  180.    _global.seenIntro = true;
  181.    this.timeline.gotoAndStop("start");
  182. };
  183. Car.prototype = new MovieClip();
  184. Car.prototype.init = function(acceleration, maxspeed, maxsteering, numcars, numkerbs, numhitpoints)
  185. {
  186.    this.accel = acceleration;
  187.    this.maxspeed = maxspeed;
  188.    this.maxsteering = maxsteering;
  189.    this.numparkedcars = numcars;
  190.    this.numkerbs = numkerbs;
  191.    this.numhitpoints = numhitpoints;
  192.    this.maxnegspeed = -4;
  193.    this.deccel = 0.4;
  194.    this.friction = 0.97;
  195.    this.braking = 0.9;
  196.    this.maxwheelrotation = 15;
  197.    this.ymov = new Number(0);
  198.    this.ymov = new Number(0);
  199.    this.speed = new Number(0);
  200.    this.startangle = this.rotation;
  201.    this.wheelrotation = new Number(0);
  202.    this.steering = new Number(0);
  203.    this.radiance = new Number();
  204.    this.point = new Object();
  205.    this.oldx = new Number();
  206.    this.oldy = new Number();
  207.    this.oldrotation = new Number();
  208.    this.x1 = new Number();
  209.    this.y1 = new Number();
  210.    this.x2 = new Number();
  211.    this.y2 = new Number();
  212.    this.started = false;
  213.    this.onkerb = false;
  214.    this.hitkerb = new Number(0);
  215.    this.hitcar = new Number(0);
  216.    this.crashSoundPlaying = false;
  217.    this.idleSoundPlaying = false;
  218.    this.stopSignShowing = false;
  219.    this.stopSignShown = false;
  220.    this.attachSounds();
  221.    var i = 1;
  222.    while(i <= this.numparkedcars)
  223.    {
  224.       _global.game.timeline["parked" + i]._visible = false;
  225.       i++;
  226.    }
  227.    var i = 1;
  228.    while(i <= this.numkerbs)
  229.    {
  230.       _global.game.timeline["kerb" + i + "_mc"]._visible = false;
  231.       i++;
  232.    }
  233.    _global.game.timeline.target_mc._visible = false;
  234.    this._x = this.carx;
  235.    this._y = this.cary;
  236.    this._rotation = this.rotation;
  237.    this.frontwheelleft_mc._rotation = 0;
  238.    this.frontwheelright_mc._rotation = 0;
  239.    if(this._currentframe > 1)
  240.    {
  241.       this.gotoAndStop(1);
  242.    }
  243.    Key.addListener(this);
  244. };
  245. Car.prototype.startEngine = function()
  246. {
  247.    this.started = true;
  248.    _global.game.timeline.presszstart_mc.play();
  249.    this.gotoAndPlay("enginestart");
  250. };
  251. Car.prototype.startMovement = function()
  252. {
  253.    this.startIdleSound();
  254.    _global.game.timeline.parkingspace_mc._visible = false;
  255.    _global.game.timeline.timer_mc.startCountdown();
  256.    this.onEnterFrame = this.calculateMove;
  257. };
  258. Car.prototype.stopEngine = function()
  259. {
  260.    this.started = false;
  261.    _global.game.timeline.timer_mc.stopCountdown();
  262.    this.smoke_mc._visible = false;
  263.    if(this.stopSignShowing)
  264.    {
  265.       _global.game.timeline.presszstop_mc.play();
  266.    }
  267.    this.stopIdleSound();
  268.    this.onEnterFrame = undefined;
  269.    Key.removeListener(this);
  270.    this.checkParking();
  271. };
  272. Car.prototype.checkParking = function()
  273. {
  274.    var points = 0;
  275.    var targetclip = _global.game.timeline.target_mc;
  276.    var minx = targetclip._x;
  277.    var maxx = minx + targetclip._width;
  278.    var miny = targetclip._y;
  279.    var maxy = miny + targetclip._height;
  280.    var i = 1;
  281.    while(i <= this.numhitpoints)
  282.    {
  283.       this.point.x = this["hit" + i]._x;
  284.       this.point.y = this["hit" + i]._y;
  285.       this.localToGlobal(this.point);
  286.       if(this.point.x > minx && this.point.x < maxx && this.point.y > miny && this.point.y < maxy)
  287.       {
  288.          points += 1;
  289.       }
  290.       i++;
  291.    }
  292.    var percent = Math.round(points / this.numhitpoints * 100);
  293.    _global.game.calculateScore(percent,this.hitcar,this.hitkerb,false);
  294. };
  295. Car.prototype.onKeyUp = function()
  296. {
  297.    if(Key.getCode() === 90)
  298.    {
  299.       if(this.started)
  300.       {
  301.          this.stopEngine();
  302.       }
  303.       else
  304.       {
  305.          this.startEngine();
  306.       }
  307.    }
  308. };
  309. Car.prototype.renderMove = function()
  310. {
  311.    this.xmov = this.speed * Math.sin(this.radiance);
  312.    this.ymov = this.speed * Math.cos(this.radiance);
  313.    this._x += this.xmov;
  314.    this._y -= this.ymov;
  315.    this.radiance = this._rotation * 0.017453292519943295;
  316.    this.radiance += this.steering * this.speed / 40;
  317.    this._rotation = this.radiance * 57.29577951308232;
  318.    this.frontwheelleft_mc._rotation = this.wheelrotation;
  319.    this.frontwheelright_mc._rotation = this.wheelrotation;
  320. };
  321. Car.prototype.calculateMove = function()
  322. {
  323.    this.memox = this._x;
  324.    this.memoy = this._y;
  325.    this.memorot = this._rotation;
  326.    if(Key.isDown(37))
  327.    {
  328.       this.steering -= 0.05;
  329.       this.wheelrotation -= 2.5;
  330.    }
  331.    if(Key.isDown(39))
  332.    {
  333.       this.steering += 0.05;
  334.       this.wheelrotation += 2.5;
  335.    }
  336.    if(this.steering > this.maxsteering)
  337.    {
  338.       this.steering = this.maxsteering;
  339.    }
  340.    if(this.steering < - this.maxsteering)
  341.    {
  342.       this.steering = - this.maxsteering;
  343.    }
  344.    if(this.wheelrotation > this.maxwheelrotation)
  345.    {
  346.       this.wheelrotation = this.maxwheelrotation;
  347.    }
  348.    if(this.wheelrotation < - this.maxwheelrotation)
  349.    {
  350.       this.wheelrotation = - this.maxwheelrotation;
  351.    }
  352.    if(Key.isDown(38))
  353.    {
  354.       if(this.speed < this.maxspeed)
  355.       {
  356.          this.speed += this.accel;
  357.       }
  358.    }
  359.    else if(Key.isDown(40))
  360.    {
  361.       if(this.speed <= 0)
  362.       {
  363.          if(this.speed > this.maxnegspeed)
  364.          {
  365.             this.speed -= this.deccel;
  366.          }
  367.       }
  368.       else
  369.       {
  370.          this.speed *= this.braking;
  371.       }
  372.    }
  373.    if(this.speed < 0.5 && this.speed > -0.4)
  374.    {
  375.       this.speed = 0;
  376.    }
  377.    else
  378.    {
  379.       this.speed *= this.friction;
  380.    }
  381.    this.renderMove();
  382.    if(this.checkKerbCollision())
  383.    {
  384.       if(!this.onkerb)
  385.       {
  386.          this.hitkerb = this.hitkerb + 1;
  387.          this.onkerb = true;
  388.       }
  389.    }
  390.    else
  391.    {
  392.       this.onkerb = false;
  393.    }
  394.    if(this.checkCarCollisions())
  395.    {
  396.       if(!this.crashSoundPlaying)
  397.       {
  398.          this.crash_sound.start();
  399.          this.crashSoundPlaying = true;
  400.          this.hitcar = this.hitcar + 1;
  401.       }
  402.       this.undoMove();
  403.       this.speed = 0;
  404.    }
  405.    else if(this.isOutOfBounds())
  406.    {
  407.       this.undoMove();
  408.       this.speed = 0;
  409.    }
  410.    if(!this.stopSignShown)
  411.    {
  412.       if(this.carIsInParkingSpace())
  413.       {
  414.          if(!this.stopSignShowing)
  415.          {
  416.             _global.game.timeline.presszstop_mc.play();
  417.          }
  418.       }
  419.       else if(this.stopSignShowing)
  420.       {
  421.          _global.game.timeline.presszstop_mc.play();
  422.       }
  423.    }
  424. };
  425. Car.prototype.undoMove = function()
  426. {
  427.    this._x = this.memox;
  428.    this._y = this.memoy;
  429.    this._rotation = this.memorot;
  430. };
  431. Car.prototype.isOutOfBounds = function()
  432. {
  433.    if(this._x > Stage.width || this._x < 0 || this._y > Stage.height || this._y < 0)
  434.    {
  435.       return true;
  436.    }
  437.    return false;
  438. };
  439. Car.prototype.checkKerbCollision = function()
  440. {
  441.    if(this.numkerbs > 0)
  442.    {
  443.       var i = 0;
  444.       while(i < this.numkerbs)
  445.       {
  446.          var num = 10;
  447.          var j = 1;
  448.          while(j <= num)
  449.          {
  450.             this.point.x = this["hit" + j]._x;
  451.             this.point.y = this["hit" + j]._y;
  452.             this.localToGlobal(this.point);
  453.             if(_global.game.timeline["kerb" + (i + 1) + "_mc"].hitTest(this.point.x,this.point.y))
  454.             {
  455.                return true;
  456.             }
  457.             j++;
  458.          }
  459.          i++;
  460.       }
  461.    }
  462. };
  463. Car.prototype.checkCarCollisions = function()
  464. {
  465.    var i = 1;
  466.    while(i <= this.numparkedcars)
  467.    {
  468.       var collisionsquare = _global.game.timeline["parked" + i];
  469.       if(this.checkObjectWithinRange(collisionsquare))
  470.       {
  471.          var j = 1;
  472.          while(j <= this.numhitpoints)
  473.          {
  474.             this.point.x = this["hit" + j]._x;
  475.             this.point.y = this["hit" + j]._y;
  476.             this.localToGlobal(this.point);
  477.             var minx = collisionsquare._x - collisionsquare._width / 2;
  478.             var maxx = collisionsquare._x + collisionsquare._width / 2;
  479.             var miny = collisionsquare._y - collisionsquare._height / 2;
  480.             var maxy = collisionsquare._y + collisionsquare._height / 2;
  481.             if(this.point.x > minx && this.point.x < maxx && this.point.y > miny && this.point.y < maxy)
  482.             {
  483.                var car = _global.game.timeline["car" + i + "_mc"];
  484.                if(car && car._currentframe == 1)
  485.                {
  486.                   car.gotoAndPlay("alarm");
  487.                }
  488.                return true;
  489.             }
  490.             j++;
  491.          }
  492.       }
  493.       i++;
  494.    }
  495.    return false;
  496. };
  497. Car.prototype.carIsInParkingSpace = function()
  498. {
  499.    var parkingspace = _global.game.timeline.target_mc;
  500.    if(this.checkObjectWithinRange(parkingspace))
  501.    {
  502.       var j = 1;
  503.       while(j <= this.numhitpoints)
  504.       {
  505.          this.point.x = this["hit" + j]._x;
  506.          this.point.y = this["hit" + j]._y;
  507.          this.localToGlobal(this.point);
  508.          var minx = parkingspace._x;
  509.          var maxx = parkingspace._x + parkingspace._width;
  510.          var miny = parkingspace._y;
  511.          var maxy = parkingspace._y + parkingspace._height;
  512.          if(this.point.x > minx && this.point.x < maxx && this.point.y > miny && this.point.y < maxy)
  513.          {
  514.             return true;
  515.          }
  516.          j++;
  517.       }
  518.    }
  519.    return false;
  520. };
  521. Car.prototype.checkObjectWithinRange = function(objtocheck)
  522. {
  523.    this.x1 = this._x;
  524.    this.y1 = this._y;
  525.    this.x2 = objtocheck._x;
  526.    this.y2 = objtocheck._y;
  527.    var result = (this.x1 - this.x2) * (this.x1 - this.x2) + (this.y1 - this.y2) * (this.y1 - this.y2);
  528.    if(result < this.delta)
  529.    {
  530.       return true;
  531.    }
  532.    return false;
  533. };
  534. Car.prototype.startIdleSound = function()
  535. {
  536.    if(!this.idleSoundPlaying)
  537.    {
  538.       this.idle_sound.start(0,99);
  539.       this.idleSoundPlaying = true;
  540.    }
  541. };
  542. Car.prototype.stopIdleSound = function()
  543. {
  544.    if(this.idleSoundPlaying)
  545.    {
  546.       this.idle_sound.stop();
  547.       this.idleSoundPlaying = false;
  548.    }
  549. };
  550. Car.prototype.attachSounds = function()
  551. {
  552.    if(this.crash_sound == undefined)
  553.    {
  554.       this.crash_sound = new Sound(this);
  555.       this.crash_sound.attachSound("crash");
  556.       this.crash_sound._parent = this;
  557.       this.crash_sound.onSoundComplete = function()
  558.       {
  559.          this._parent.crashSoundPlaying = false;
  560.       };
  561.    }
  562.    if(this.idle_sound == undefined)
  563.    {
  564.       this.idle_sound = new Sound(this);
  565.       this.idle_sound._parent = this;
  566.       this.idle_sound.attachSound("engine_idle");
  567.       this.idle_sound.onSoundComplete = function()
  568.       {
  569.          this._parent.idleSoundPlaying = false;
  570.       };
  571.    }
  572. };
  573. Object.registerClass("practice_car",Car);
  574. Object.registerClass("blue_hatchback",Car);
  575. Object.registerClass("black_saloon",Car);
  576. Object.registerClass("grey_suv",Car);
  577. _global.serverPath = "http://adverts.freeloader.com/zurich/_scripts/";
  578. _global.prizeDrawScript = "addtoprizedraw.php";
  579. _global.saveScoreScript = "savescore.php";
  580. _global.scoresXML = "scores.xml";
  581. _global.player_gender = new String();
  582. _global.seenIntro = false;
  583.